package com.develop.data.loan.model;

import com.develop.data.base.model.IdSpaceTimeRecord;
import com.develop.data.loan.jpa.attribute.converter.*;
import com.develop.data.loan.option.*;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;

import javax.money.MonetaryAmount;
import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.List;

@Entity
@Table(name = "LOAN_CONTRACT")
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
@Getter
@Setter
@JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class,
        property = "id"
)
public class LoanContract extends IdSpaceTimeRecord {

    //public String contractStatus; //合同状态
    public String contractNumber; //合同编号
    public String contractSequence; //合同流水号
    public String loanProductionNumber; //贷款产品编号，枚举类型合适
    public String loanBusinessNumber; //贷款业务编号，枚举类型合适
    @Convert(converter = LoanCorpusTypeAttributeConverter.class)
    public LoanCorpusType loanCorpusType;
    @Convert(converter = LoanTimelineTypeAttributeConverter.class)
    public LoanTimelineType loanTimelineType;
    public int contractRepaymentDay;
    public int contractRepaymentInterval;
    public int contractFirstTermDaysFloor;  //不足时，并入下一期
    @Convert(converter = LoanRepayFrequencyAttributeConverter.class)
    public LoanRepayFrequency loanPrincipalRepayFrequency;
    @Convert(converter = LoanRepayFrequencyAttributeConverter.class)
    public LoanRepayFrequency loanInterestRepayFrequency;
    @Convert(converter = LoanRateTypeAttributeConverter.class)
    public LoanRateType loanRateType;
    public String contractRateValue;
    @Convert(converter = LoanGuaranteeTypeAttributeConverter.class)
    public LoanGuaranteeType loanGuaranteeType;
    public int contractGracePeriod; //宽限期，单位天
    public int contractTermHead; //贷款未结清首期
    public int contractTermTail; //贷款尾期
    @Convert(converter = LoanStateAttributeConverter.class)
    public LoanState loanState; //贷款生命周期阶段
    @Convert(converter = LoanUsageAttributeConverter.class)
    public LoanUsage loanUsage; //贷款用途
    @Convert(converter = LoanRegionAttributeConverter.class)
    public LoanRegion loanRegion; //贷款投放区域
    public String customerID;
    public String customerNumber; //客户编号，Embeddable合适
    public String customerName;
    public String customerCertificateType; //证件类型
    public String customerCertificateNumber; //证件号码
    public String customerEncashAccountType; //放款账号类型
    public String customerEncashAccountNumber; //放款账号
    @Convert(converter = LoanMoneyAmountAttributeConverter.class)
    public MonetaryAmount contractEncashAmount; //放款金额
    public LocalDateTime contractEncashDate; //放款时间
    public String customerRepayAccountType; //还款账号类型
    public String customerRepayAccountNumber; //还款账号
    public String customerCreditID; //授信编号
    public String customerCreditDate; //授信时间
    public LocalDateTime contractValueDate; //起息日
    public LocalDateTime contractMaturityDate; //到期日

    public boolean cleared=false;
    public int daysCleared(){return 0;}
    public boolean overdue=false;
    public boolean graced; /*宽限期*/
    public int daysOverdue(){return 0;}
    public boolean accrual=true;
    public int daysNonAccrual(){return 0;}
    public boolean cancel=false;
    public int daysCanceled(){return 0;}
    public boolean expired=false;
    public int daysExpired(){return 0;}

    public LocalDateTime instantCleared;
    public LocalDateTime instantOverDue;
    public LocalDateTime instantFrozen;

    public LocalDateTime accountInstantNonAccrual ;
    public LocalDateTime accountInstantCancel;



    /*@OneToMany
    @JoinColumn(name="loanContract")*/
    @Getter
    //@OneToMany(mappedBy = "loanContract", cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE, CascadeType.REFRESH})
    @OneToMany(mappedBy = "loanContract", cascade = {CascadeType.ALL})
    @Convert(converter = LoanInstallmentListAttributeConverter.class)
    public List<LoanInstallment> loanInstallments;

    @Getter
    //@OneToMany(mappedBy = "loanContract", cascade = {CascadeType.PERSIST,CascadeType.MERGE,CascadeType.REMOVE, CascadeType.REFRESH})
    //@OneToMany(mappedBy = "loanContract", cascade = {CascadeType.ALL})
    @OneToMany(cascade = {CascadeType.ALL})
    @Convert(converter = LoanRepaymentListAttributeConverter.class)
    public List<LoanRepayment> loanRepayments;

    /*public void addLoanInstallment(LoanInstallment loanInstallment){
        loanInstallments.add(loanInstallment);
        loanInstallment.setLoanContract(this);
    }

    public void removeLoanInstallment(LoanInstallment loanInstallment){
        loanInstallments.remove(loanInstallment);
        loanInstallment.setLoanContract(null);
    }*/

    @Override
    public String toString(){
        return id;
    }
}
